package action;

import java.text.DateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import bean.Status;
import bean.Travelogue;
import bean.User;
import dao.StatusDAO;
import dao.TravelogueDAO;
import dao.UserDAO;
import form.TravelogueForm;

public class UploadTravelogueAction extends Action {
	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		TravelogueForm tform=(TravelogueForm)form;
		String logTitle=tform.getLogName();
		String content=tform.getLogComment();
		
		UserDAO userDAO=new UserDAO();
		TravelogueDAO travelogueDAO=new TravelogueDAO();
		ActionErrors errors=new ActionErrors();
		HttpSession session=request.getSession(false);

		User user=(User)session.getAttribute("user");

		if(user!=null){
			Date now = new Date();
			DateFormat.getInstance().format(now);
			System.out.println(""+now);
		    Travelogue newTravelogue=new Travelogue(user,logTitle,content,now);
		    travelogueDAO.save(newTravelogue);
		    session.setAttribute("travelogue", newTravelogue);
		    return mapping.findForward("listTravelogue");
		}
		else{
			errors.add("notExist",new ActionMessage("TravelogueUser.notExist"));
		}
		
		saveErrors(request, errors);
		return mapping.getInputForward();
		
			
	}

}
